package com.voxel.sense.infomsg.widget.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.com.common.utils.FileUtils;
import com.example.com.common.utils.ScreenUtils;
import com.example.com.common.utils.SizeUtils;
import com.example.com.common.view.adapter.TBaseAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.FileImageInfo;
import cyy.example.com.facotry.constants.Constants;
import com.voxel.sense.infomsg.interfaces.listener.onMyItemClickListener;
import com.voxel.sense.infomsg.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ChenYiYao on 2017/11/24.
 */

public class FileGalleryAdapter extends TBaseAdapter<FileImageInfo> {
    RelativeLayout.LayoutParams layoutParams;
    private int mImageSize;
    private int file_type = Constants.file_iage;//默认是图片
    private int max_num = Constants.UPLOAD_FILE_MAX_COUNT; //最多能够选择文件的数量
    private boolean isUpComplate = false; //标记是否上传文件完成
    private onMyItemClickListener mDeleteListener;


    public FileGalleryAdapter(Context context, List<FileImageInfo> list) {
        super(context, list, R.layout.item_gridview_addpic);
        this.context = context;
        int pad = SizeUtils.dp2px(2);
        mImageSize = (ScreenUtils.getScreenWidth() - pad) / 4;
        //设置item 的大小
        layoutParams = new RelativeLayout.LayoutParams(mImageSize, mImageSize);
    }



    @Override
    public void initView(ViewHolder holder) {
        holder.addView(R.id.iv_pic);
        holder.addView(R.id.iv_delete);
        holder.addView(R.id.rl);
    }

    @Override
    public void setViewValue(ViewHolder holder, final int position) {
        FileImageInfo fileImageInfo = list.get(position);
        ImageView iv_pic = holder.getImageView(R.id.iv_pic);
        iv_pic.setLayoutParams(layoutParams);
        ImageView iv_delete = holder.getImageView(R.id.iv_delete);
        View parent = holder.getParent();
        //点击删除图标，移除文件
        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteFile(position);
                if (mDeleteListener!=null){
                    mDeleteListener.onItemClick(position);
                }
            }
        });
        loadItemInfo(file_type, fileImageInfo, parent, iv_pic, iv_delete, R.mipmap.add_image);

    }

    private void loadItemInfo(int file_type, FileImageInfo fileImageInfo, View parent, ImageView iv_pic, ImageView iv_delete, int addIconResId) {
        if (Constants.ADD_FILE.equals(fileImageInfo.fileName)) {
            //添加的item
            initAddFileView(parent, iv_pic, iv_delete, addIconResId);
        } else {
            //文件item
            parent.setVisibility(View.VISIBLE);
            iv_pic.setVisibility(View.VISIBLE);
            if (isUpComplate) {
                iv_delete.setVisibility(View.GONE);
            } else {
                iv_delete.setVisibility(View.VISIBLE);
            }

            switch (file_type) {
                case Constants.file_docu://文档
                    String fileExtension = FileUtils.getFileExtension(fileImageInfo.filePath);
                    loadDocuIcon(iv_pic, fileExtension);
                    break;
                case Constants.file_vedio://视频
                    GlideUtils.GlideLoadPic(context, R.mipmap.mp4_icon, iv_pic);
                    break;
                default://默认类型图片
                    GlideUtils.GlideLoadPic(context, fileImageInfo.filePath, iv_pic);
                    break;
            }
        }
    }

    //初始化icon
    private void loadDocuIcon(ImageView iv_pic, String fileExtension) {
        //TODO:由于icon缺失，暂时选择word文档的icon
        if (Constants.EXTENSION_DOC.equals(fileExtension) || Constants.EXTENSION_DOCX.equals(fileExtension)) {
            //doc文档
            GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
        } else if (Constants.EXTENSION_XLS.equals(fileExtension) || Constants.EXTENSION_XLSX.equals(fileExtension)) {
            //xls文档
            GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
        } else if (Constants.EXTENSION_PDF.equals(fileExtension)) {
            //pdf文档
            GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
        } else if (Constants.EXTENSION_PPT.equals(fileExtension) || Constants.EXTENSION_PPTX.equals(fileExtension)) {
            //ppt文档
            GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
        } else {
            //默认显示文本
            GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
        }
    }

    //初始化添加按钮
    private void initAddFileView(View parent, ImageView iv_pic, ImageView iv_delete, int addIconResId) {
        //选择的文件没有超过最大限制以及文件没有上传时，添加按钮就显示
        if (list.size() <= max_num && !isUpComplate) {
            //选择的图片还没有添加到最大的限制
            GlideUtils.GlideLoadPic(context, addIconResId, iv_pic);
            iv_delete.setVisibility(View.GONE);
            iv_pic.setVisibility(View.VISIBLE);
            parent.setVisibility(View.VISIBLE);
        } else {
            //选择的图片已到达限定的数量，隐藏该item
            parent.setVisibility(View.GONE);
            iv_pic.setVisibility(View.GONE);
            iv_delete.setVisibility(View.GONE);
            return;
        }
    }

    //删除
    private void deleteFile(int position) {
        list.remove(position);
        notifyDataSetChanged();
    }

    public int getFile_type() {
        return file_type;
    }

    public void setFile_type(int file_type) {
        this.file_type = file_type;
    }

    public int getMax_num() {
        return max_num;
    }

    public void setMax_num(int max_num) {
        this.max_num = max_num;
    }

    public void setUpComplate(boolean upComplate) {
        isUpComplate = upComplate;
        notifyDataSetChanged();
    }

    @Override
    public void setList(List<FileImageInfo> list) {
        if (list != null) {
            list.add(list.size(), new FileImageInfo(Constants.ADD_FILE, Constants.ADD_FILE));
        }
        super.setList(list);
    }

    public void setOnDeleteListener(onMyItemClickListener listener){
        this.mDeleteListener =listener;
    }

    public ArrayList<String> getDatasPath() {
        ArrayList datas = new ArrayList();
        for (int i =0 ; i< list.size()-1;i++){
            datas.add(list.get(i).filePath);
        }
        return datas;
    }
}
